//封装分类组件相关的业务
import {ref,onMounted} from 'vue'
import {onBeforeRouteUpdate} from 'vue-router'
import { useRoute } from 'vue-router'
import { getCategoryAPI } from '@/apis/category'
export function useCategory(){
    const categoryData = ref({})
    const route = useRoute()
    const getCategory = async (id=route.params.id) => {
      const res = await getCategoryAPI(id)
      categoryData.value = res.result
    }
    onMounted(() => getCategory())
  //路由参数变化，可以把分类接口重新发送
  onBeforeRouteUpdate((to)=>{
    //使用最新的路由参数请求
   getCategory(to.params.id)
  })
  return{
    categoryData
  }
}